<template>
  <div class="personal-center">
    <!-- 顶部欢迎栏 -->
    <div class="welcome-bar">您好，欢迎进入个人中心！</div>
    <!-- 用户信息区域 -->
    <div class="user-info">
      <img src="@/assets/images/OIP-C.jpg" alt="订花乐logo" class="user-avatar" />
      <div class="user-titles">
        <div class="user-title">订花乐用户</div>
        <div class="user-subtitle">订花乐游客</div>
      </div>
    </div>
    <!-- 订单状态 tabs -->
    <div class="order-tabs">
      <div class="tab active">全部订单</div>
      <div class="tab">待付款</div>
      <div class="tab">派送中</div>
      <div class="tab">待评价</div>
      <div class="tab">已完成</div>
    </div>
    <!-- 订单表头 -->
    <div class="order-header">
      <div class="header-item">订单信息</div>
      <div class="header-item">商品图片</div>
      <div class="header-item">商品信息</div>
      <div class="header-item">总数量</div>
      <div class="header-item">总价</div>
      <div class="header-item">状态</div>
      <div class="header-item">操作</div>
    </div>
    <!-- 订单列表（此处可根据实际需求循环渲染订单数据） -->
    <div class="order-list">
      <!-- 示例：暂无订单 -->
      <div class="empty-tip" v-if="orderList.length === 0">暂无订单数据</div>
      <!-- 若有订单数据，可通过 v-for 渲染，示例如下（需根据实际数据结构调整） -->
        <div class="order-item" v-for="(order, index) in orderList" >
          <div class="order-info">{{ order.order_id }}</div>
          <div class="order-info" ><img :src="order.flowers[0].image"/></div>
          <div class="order-info" >
            <div v-for="flower in order.flowers" >{{ flower.use }}--{{ flower.material }}</div>
          </div>
          <div class="order-info"  >{{ totalcount(order) }}</div>
          <div class="order-info"  >{{ totalprice(order)}}</div>
          <div class="order-info"  >已支付</div>
          <div class="order-info">
            <button class="operate-btn">操作</button>
          </div>
        </div>
    </div>
    <!-- 分页 -->
      <div v-if="data.total >= 0" class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="data.pageNum"
          :page-sizes="[1, 2, 3, 5]"
          :page-size="data.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="data.total"
        ></el-pagination>
      </div>

  </div>
</template>


<script setup>
import { ref, onMounted} from 'vue';
// 可根据实际需求引入 Axios 等进行数据请求，这里假设使用 Axios
import { useLoginStore } from "@/stores/LoginStore";
import { ElMessage } from 'element-plus';
import axios from '@/http/axios';
const loginStore = useLoginStore();
const orderList = ref([]);
const data = ref({
  total: 3,
  pageSize: 3,
  list: [],
  pageNum: 1
});
const loading = ref(false); // 添加缺失的加载状态变量

const totalprice = (order)=>{
  var sum=0
  for(var i=0;i<order.flowers.length;i++){
    var a = order.flowers[i].price 
    var b = order.counts[i]
    sum+=a*b
  }
  return sum
}
const totalcount = (order)=>{
  var sum=0
  for(var i=0;i<order.counts.length;i++){
    var a = order.counts[i]
    sum+=a
  }
  return sum
}
// 分页相关方法
const handleSizeChange = (val) => {
  data.value.pageSize = val;
  data.value.pageNum = 1;
  fetchOrders();
};

const handleCurrentChange = (val) => {
  data.value.pageNum = val;
  fetchOrders();
};

import { useRouter } from 'vue-router';
const router = useRouter()
// 返回按钮处理
const handleBack = () => {
  ElMessage.info('返回上一页');
  router.back()
};

onMounted(async () => {
  await fetchOrders(); // 直接调用统一的数据获取方法
});

// 获取订单数据（统一处理所有数据获取逻辑）
const fetchOrders = async () => {
  loading.value = true;
  
  try {
    var res = await axios.get('/order/list/'+data.value.pageNum+'/'+data.value.pageSize)
    data.value = res.data.data;
    orderList.value = data.value.list; // 同步更新用于渲染的数据
    
    // 默认展开第一个订单（如果需要）
    // if (data.value.list.length > 0) {
    //   activeNames.value = [data.value.list[0].order_id];
    // }
  } catch (error) {
    console.error('获取订单数据失败：', error);
    ElMessage.error('获取订单数据失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

</script>


<style scoped>
.personal-center {
  width: 100%;
  font-family: "Microsoft Yahei", sans-serif;
}

/* 欢迎栏样式 */
.welcome-bar {
  background-color: #d3d3d3;
  padding: 10px;
  font-size: 16px;
  color: #333;
  width: 100%;
  box-sizing: border-box;
}

/* 用户信息区域 */
.user-info {
  display: flex;
  align-items: center;
  padding: 15px;
}

.user-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-right: 10px;
}

.user-titles .user-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.user-titles .user-subtitle {
  font-size: 14px;
  color: #666;
}

/* 订单 tabs 样式 */
.order-tabs {
  display: flex;
  border-bottom: 1px solid #e5e5e5;
  margin: 0 10px;
}

.tab {
  padding: 10px 20px;
  cursor: pointer;
  color: #666;
  position: relative;
  flex: 1;
  text-align: center;
}

.tab.active {
  color: #ff0000;
}

.tab.active::after {
  content: "";
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #ff0000;
}

/* 订单表头样式 */
.order-header {
  display: flex;
  background-color: #f9f9f9;
  border-bottom: 1px solid #e5e5e5;
  margin: 0 10px;
}

.header-item {
  flex: 1;
  text-align: left;
  color: #666;
}

/* 订单列表样式优化 */
.order-list {
  margin: 0 10px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.empty-tip {
  color: #999;
  font-size: 14px;
  text-align: center;
  padding: 60px 0;
  background-color: #fafafa;
  border-radius: 4px;
}

/* 订单项样式优化 */
.order-item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid #e5e5e5;
  transition: background-color 0.3s ease;
}

.order-item:hover {
  background-color: #f9f9f9;
}

/* 订单信息样式优化 */
.order-info {
  flex: 1;
  display: flex;
  justify-self: center;
  align-items: center;
  font-size: 14px;
  color: #333;
}

/* 订单ID样式 */
.order-info:nth-child(1) {
  font-weight: 500;
  color: #606266;
}

/* 商品图片样式 */
.order-info:nth-child(2) {
  justify-content: center;
}

.order-info:nth-child(2) img {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #e4e7ed;
}

/* 商品ID列表样式 */
.order-info:nth-child(3) {
  flex-direction: column;
  align-items: flex-start;
}

.order-info:nth-child(3) div {
  margin-bottom: 2px;
  color: #606266;
}

/* 数量和价格样式 */
.order-info:nth-child(4),
.order-info:nth-child(5) {
  font-weight: 500;
  color: #303133;
}

.order-info:nth-child(5) {
  color: #f56c6c;
}

/* 状态样式 */
.order-info:nth-child(6) {
  font-weight: 500;
}

/* 已支付状态 */
.order-info:nth-child(6):contains('已支付') {
  color: #67c23a;
}

/* 待付款状态 */
.order-info:nth-child(6):contains('待付款') {
  color: #e6a23c;
}

/* 派送中状态 */
.order-info:nth-child(6):contains('派送中') {
  color: #409eff;
}

/* 待评价状态 */
.order-info:nth-child(6):contains('待评价') {
  color: #909399;
}

/* 已完成状态 */
.order-info:nth-child(6):contains('已完成') {
  color: #909399;
}

/* 操作按钮样式 */
.order-info:nth-child(7) {
  justify-content: center;
}

.operate-btn {
  padding: 6px 16px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.operate-btn:hover {
  background-color: #66b1ff;
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
}

.operate-btn:active {
  transform: translateY(0);
}

/* 调整表头样式以匹配内容 */
.order-header {
  display: flex;
  background-color: #f5f7fa;
  padding: 12px 10px;
  border-bottom: 1px solid #e5e5e5;
  margin: 0 10px;
  font-weight: 500;
  color: #606266;
}

.header-item {
  flex: 1;
  text-align: left;
  padding: 0 5px;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .order-tabs {
    overflow-x: auto;
    white-space: nowrap;
  }
  
  .order-header,
  .order-item {
    font-size: 12px;
  }
  
  .order-info:nth-child(2) img {
    width: 40px;
    height: 40px;
  }
  
  .operate-btn {
    padding: 4px 12px;
    font-size: 12px;
  }
}
 .pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>